package com.foreknow.controller;

import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //向网页响应内容  字符流
        PrintWriter out = resp.getWriter();
        GuestbookService guestbookService = new GuestbookServiceImpl();
        List<Object> list = guestbookService.allquery();
        //如何将list集合保存到一个地方,转发之后可以在list.jsp中方便获取(保存在服务器端)
        req.setAttribute("list",list);
        //当前页面跳转list.jsp页面
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
//        out.println("<html>");
//        out.println("<head>");
//        out.println("<title>guestbook");
//        out.println("</title>");
//        out.println("</head>");
//        out.println("<body>");
//        out.println("<table border='1'>");
//        out.println("<tr>");
//        out.println("<th>id</th>");
//        out.println("<th>name</th>");
//        out.println("<th>phone</th>");
//        out.println("<th>email</th>");
//        out.println("<th>title</th>");
//        out.println("<th>content</th>");
//        out.println("<th>createdtime</th>");
//        out.println("</tr>");
//        for (int i = 0; i < list.size(); i++) {
//            Guestbook guestbook = (Guestbook) list.get(i);
//            out.println("<tr>");
//            out.println("<td>" + guestbook.getId() + "</td>");
//            out.println("<td>" + guestbook.getName() + "</td>");
//            out.println("<td>" + guestbook.getPhone() + "</td>");
//            out.println("<td>" + guestbook.getEmail() + "</td>");
//            out.println("<td>" + guestbook.getTitle() + "</td>");
//            out.println("<td>" + guestbook.getContent() + "</td>");
//            out.println("<td>" + guestbook.getCreatedtime() + "</td>");
//            out.println("</tr>");
//        }
//        out.println("</table>");
//        out.println("</body>");
//        out.println("</html>");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}